๐Ÿงฉ Part 1: Wrapper Classes in Java

๐Ÿ”ท What is a Wrapper Class?

A Wrapper class is a class whose object wraps a primitive data type. Java provides these classes in the java.lang package to convert primitive types into objects.

๐Ÿงฑ Primitive Types vs Wrapper Classes

Primitive Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

๐Ÿ” Why Wrapper Classes are Needed?

Key Reasons:

  • Collections API: Like ArrayList, HashMap can only store objects (not primitives)
  • Utilities: Wrapper classes have methods like parseInt(), compare(), valueOf()
  • Autoboxing/Unboxing: Java automatically converts between primitives and wrappers
  • Can be null: Useful when working with databases where a value may be unknown
  • Generic Support: Generics in Java (like <T>) do not support primitives, only objects

๐Ÿ” Autoboxing and Unboxing

Autoboxing = primitive โžก๏ธ object

Unboxing = object โžก๏ธ primitive

public class AutoBoxingExample {
    public static void main(String[] args) {
        int a = 10;

        // Autoboxing
        Integer obj = a;

        // Unboxing
        int b = obj;

        System.out.println("Object: " + obj);
        System.out.println("Primitive: " + b);
    }
}

๐Ÿ’ผ Real-Life Example

Let's say you want to store user ages in a list:

ArrayList<Integer> ages = new ArrayList<>();
ages.add(25); // Autoboxing happens here: int โž Integer
ages.add(30);

int sum = ages.get(0) + ages.get(1); // Unboxing: Integer โž int
System.out.println("Sum of ages: " + sum);

๐Ÿ› ๏ธ Useful Wrapper Class Methods

public class WrapperMethods {
    public static void main(String[] args) {
        String number = "100";

        int value = Integer.parseInt(number); // Convert string to int
        System.out.println("Parsed value: " + value);

        String text = Integer.toString(1234); // Convert int to string
        System.out.println("String value: " + text);

        boolean result = Boolean.parseBoolean("true");
        System.out.println("Boolean value: " + result);

        char ch = 'A';
        System.out.println("Is Digit: " + Character.isDigit(ch));
        System.out.println("Is Uppercase: " + Character.isUpperCase(ch));
    }
}

๐Ÿ“Œ Interview Point of View (Wrapper Class)

๐Ÿ”น Question ๐Ÿ’ฌ Expected Answer
What is a wrapper class? A class that converts a primitive type into an object.
Why are wrapper classes needed? For collections, generics, null handling, utilities.
Difference between Integer and int? int is primitive, Integer is a class (object).
What is autoboxing/unboxing? Auto conversion between primitive and wrapper.
Can you store int in ArrayList? Not directly โ€” it uses Integer via autoboxing.

๐Ÿ“˜ Generics in Java

๐Ÿ”น What is Generics?

Generics allows writing code that works with different data types while providing type safety.

Introduced in: Java 5

๐Ÿ”น Why Use Generics?

  • โœ… Type safety
  • โœ… Reusability
  • โœ… Compile-time checking
  • โœ… No need for type casting

๐Ÿ”น Generic Class Example

class Box<T> {
    T value;
    void setValue(T val) { value = val; }
    T getValue() { return value; }
}
    

๐Ÿ”น Using Generic Class

Box<String> s = new Box<>();
s.setValue("Hi");
System.out.println(s.getValue());
    

๐Ÿ”น Generic Method

public <T> void print(T[] arr) {
    for (T t : arr)
        System.out.println(t);
}
    

๐Ÿ”น Bounded Type

class Demo<T extends Number> {
    void show(T n) {
        System.out.println(n.doubleValue());
    }
}
    

๐Ÿ”น Wildcards

  • <?> โ€“ unknown type
  • <? extends Number> โ€“ any subclass of Number
  • <? super Integer> โ€“ any superclass of Integer

โœ… Summary

  • Generic = Type-safe reusable code
  • Used in collections, utilities, containers
  • Makes code more readable and safer
๐Ÿ’ก Part 2: Why Java is Not Fully Object-Oriented

โ“ What is a Fully Object-Oriented Language?

A fully object-oriented language is one where:

Examples: Smalltalk, Ruby, Scala

โŒ Why Java is Not 100% Object-Oriented?

Reason Explanation
Primitive Types Java has int, float, char, etc., which are not objects.
Static Methods/Variables Methods like Math.sqrt() don't need objects.
Limited Operator Overloading Custom operator overloading is not allowed.
Top-Level Code Outside Objects Static context used before object creation (e.g., main method).

๐Ÿ“˜ Real-World Analogy

๐Ÿ”ง Toolkit Analogy

Think of a programming language like a toolkit.

In Java, sometimes it uses tools outside the object world (like int instead of Integer), which breaks the pure object-oriented rule.

Whereas in Smalltalk:

3 + 4   "Even 3 is an object, and + is a message"

๐Ÿ”„ Comparison: Java vs Pure OOP

๐ŸŸก Java (Hybrid)

  • Has primitive types
  • Static methods exist
  • Performance optimized
  • Easier to learn

๐ŸŸข Pure OOP (Smalltalk)

  • Everything is an object
  • All operations via messages
  • Consistent design
  • More complex

๐Ÿ“Œ Interview Point of View (Object-Oriented)

๐Ÿ”น Question ๐Ÿ’ฌ Expected Answer
Is Java fully object-oriented? No, because it uses primitive types and static members.
Why does Java have primitives? For performance โ€“ primitives are faster and use less memory.
What makes Java object-oriented? Class-based, supports encapsulation, inheritance, polymorphism, and abstraction.
What breaks pure OOP in Java? int, boolean, static methods/fields, etc.

๐Ÿงพ Final Summary

โœ… Wrapper Class Summary

โœ… Java Is Not Fully OOP Because:

๐Ÿง  Quick Revision for Interviews